import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

import java.util.Base64;
import java.util.Collection;

import org.apache.avro.Schema;
import lib.avro.Avro;
import org.apache.avro.generic.GenericContainer;

public class App {
  public static byte[] gzip(byte[] avro_bin) throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GZIPOutputStream gz = new GZIPOutputStream(os);
    gz.write(avro_bin);
    gz.close();
    return os.toByteArray();
  }

  public static void main(String[] args) throws Exception {
    /*
     * // 1. request token
     * String token_str =
     * Request.Post("http://10.25.21.40:3030/signature/password_token")
     * .bodyString("{\"app_id\": \"chinapex\", \"app_secret\": \"chinapex\"}",
     * ContentType.APPLICATION_JSON)
     * .execute()
     * .returnContent()
     * .asString();
     * 
     * Map<String, Object> token_json = new ObjectMapper().readValue(token_str,
     * Map.class);
     * String access_token = (String) ((Map<String, Object>)
     * token_json.get("result")).get("access_token");
     * System.out.println("access_token:" + access_token);
     */

    // 2. push data

    /*
     * Schema schema = Protocol.parse(new
     * File("avsc/data_buffer.avsc")).getType(args[0]);
     * System.err.println(schema);
     */

    String schemaText = Avro.readAvscSchema("meta-lib-static/avdl/data_buffer.avdl:dcSdkPlatformBulk").toString();
    Schema bulkSchema = Avro.parseAvsc(schemaText);

    String androidViewPageSample = Avro
        .patchIos(Avro.slurpFile("meta-lib-static/sample/android.EventViewPage.pretty.json"));

    String iosEventViewPageSample = Avro
        .patchIos(Avro.slurpFile("meta-lib-static/sample/ios.EventViewPage.pretty.json"));

    String webClickSample = Avro.slurpFile("meta-lib-static/sample/web.EventClick.pretty.json");
    String webViewPageSample = Avro.slurpFile("meta-lib-static/sample/web.EventViewPage.pretty.json");
    String webCustomSample = Avro.slurpFile("meta-lib-static/sample/web.EventCustom.pretty.json");

    String wechatClickSample = Avro.slurpFile("meta-lib-static/sample/wechat.EventClick.pretty.json");
    String wechatViewPageSample = Avro.slurpFile("meta-lib-static/sample/wechat.EventViewPage.pretty.json");

    GenericContainer avro = Avro.fromJson(bulkSchema, androidViewPageSample);
    String json = Avro.toJson(bulkSchema, avro);
    System.out.println("json:" + json);

    // System.out.println(Base64.getEncoder().encodeToString(jsonToAvroBin(schema_str,
    // data_str))) ;

    /*
     * String push_response = Request.Post(
     * "http://10.25.21.40:3031/logging//sys/dc/bucket/sdk/avro_buffer_echo")
     * .addHeader("Authorization", "Bearer " + access_token)
     * .addHeader("Content-Type", "application/octet-steam")
     * .bodyByteArray(gzip(jsonToAvroBin(schema_str, data_str)))
     * .execute()
     * .returnContent()
     * .asString();
     */

  }
}
